package br.edu.ifrn.utineo.web;

import br.edu.ifrn.utineo.jdbc.Conexao;
import br.edu.ifrn.utineo.modelo.Funcionario;
import br.edu.ifrn.utineo.modelo.Sistema;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    Sistema sis = Sistema.getInstance();

    public LoginServlet() {
       super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          String usuario = request.getParameter("login");
          String pass = request.getParameter("senha");
          Sistema sis = Sistema.getInstance();
          if(usuario==null || pass==null){
              usuario = "vazio";
              pass = "vazio";
          }

          try {
                Conexao cnx = new Conexao();
                
                int indexFunc = cnx.obterIndexFunc(usuario, pass);
           //     int indexPac = cnx.obterIndexPac(usuario, pass);


                cnx.login(usuario, pass);
                
                String UserTemp = sis.getFuncionario(indexFunc).getNome();
                String PassTemp = sis.getFuncionario(indexFunc).getSenha();
                int CodFuncionario = sis.getFuncionario(indexFunc).getCodigoFuncionario();

                if((usuario.equalsIgnoreCase(UserTemp)) && (pass.equalsIgnoreCase(PassTemp)) && (CodFuncionario == 0))
                    response.sendRedirect("administrador/cadastrar_usuario.jsp");
                else if((usuario.equalsIgnoreCase(UserTemp)) && (pass.equalsIgnoreCase(PassTemp)) && (CodFuncionario == 1)){
                   
                        request.setAttribute("medico",sis.getFuncionario(indexFunc) );
       //                 request.setAttribute("paciente",sis.getPaciente(indexPac) );
                        RequestDispatcher dispatcher = request.getRequestDispatcher("medico/cadastrar_paciente.jsp");
			            dispatcher.forward(request, response);
			return ;
                   
                }

                else if((usuario.equalsIgnoreCase(UserTemp)) && (pass.equalsIgnoreCase(PassTemp)) && (CodFuncionario == 2))
                    response.sendRedirect("farmacia/visualizar_prescricao.jsp");

                else{
                    response.sendRedirect("index.jsp");
                }

        } catch (SQLException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
